//
//  DBRefreshView.swift
//  大波微博swift
//
//  Created by 大波 on 16/3/8.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

class DBRefreshView: UIView {

    /// 下拉刷新提示图片
    @IBOutlet weak var tipIcon: UIImageView!
    
    /// 加载中的显示动画图片
    @IBOutlet weak var loadIcon: UIImageView!
    
    /// 下拉刷新提示文字
    @IBOutlet weak var tipLabel: UILabel!
    
    /// 提示View
    @IBOutlet weak var tipView: UIView!
    
    class func loadRefresh() -> DBRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("DBRefreshView", owner: nil, options: nil).last as! DBRefreshView
    }
    
    func rotationTipIcon(isUp: Bool) {
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.tipIcon.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        
        tipLabel.text = isUp ? "释放更新" : "下拉刷新"
    }
    
    func beginLoadIconAnimate() {
        
        // 动画的key
        let animateKey = "sb"
        
        if let _ = loadIcon.layer.animationForKey(animateKey) {
            
            return
        }
        
        tipView.hidden = true
        
        let rotationAnimate = CABasicAnimation(keyPath: "transform.rotation")
        
        rotationAnimate.repeatCount = MAXFLOAT
        
        rotationAnimate.toValue = M_PI * 2
        
        rotationAnimate.duration = 0.25
        
        rotationAnimate.removedOnCompletion = false
        
        loadIcon.layer.addAnimation(rotationAnimate, forKey: animateKey)
    }
    
    func endLoad() {
        
        loadIcon.layer.removeAllAnimations()
        
        tipView.hidden = false
    }
}
















